#include "progressdialog.h"
#include "ui_progressdialog.h"


//struct resultStruct {QString filename; int perc;};
//QList<resultStruct> results(0);


ProgressDialog::ProgressDialog(QWidget *parent, QString file, QString dir) :
    QDialog(parent),
    ui(new Ui::ProgressDialog)
{
    ui->setupUi(this);
    connect(ui->cancelButton,SIGNAL(clicked()),this,SLOT(close()));
    this->mainwindow = parent;
    this->thread = new MyThread(file, dir);
    connect(thread,SIGNAL(change(const QString &)),this, SLOT(changeLabel(const QString &)));
    connect(thread,SIGNAL(bump(int)), this,SLOT(bumpPB(int)));
    connect(thread,SIGNAL(total(int)), this,SLOT(totalPB(int)));
    connect(thread, SIGNAL(finished()),this, SLOT(showResults()));
    thread->start();
}

ProgressDialog::~ProgressDialog()
{
    delete ui;
    //this->thread->exit();
    this->thread->terminate();
    this->thread->wait();
    delete this->thread;
}

void ProgressDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void ProgressDialog::changeLabel(QString text){
    ui->fileLabel->setText("<i>Обрабатывается файл: "+text);
};

void ProgressDialog::closeThis(){
    //this->closeEvent();
};

void ProgressDialog::bumpPB(int value){
    ui->progressBar->setValue(value+1);
};

void ProgressDialog::totalPB(int value){
    ui->progressBar->setMaximum(value);
}

void ProgressDialog::showResults()
{
    if (thread->resName.size() != 0)
        {
        ResultsDialog dialog(this->mainwindow);
        dialog.resName = thread->resName;
        dialog.resPerc = thread->resPerc;
        dialog.directory = thread->dir;
        dialog.image = thread->file;
        dialog.setSourceImage(thread->file);
        dialog.populateTable();
        this->close();
        dialog.exec();
        }
    else
    {
        QMessageBox::warning(this, tr("Результат поиска"), tr("Ничего не найдено!"),QMessageBox::Ok);
        this->close();
    }

}
